import React from 'react'

// 处理错误边界的高阶组件
export default function ErrorBoundary(WrappedComponent: any, source: string) {
  return class ErrorBoundary extends React.Component {
    state = { hasError: false, error: null }

    static getDerivedStateFromError(error) {
      return { hasError: true, error: error }
    }

    render() {
      if (this.state.hasError) {
        return (
          <div>
            <div>Error from &lt;{source}/&gt; component：</div>
            <p> {this.state.error.message} </p>
          </div>
        )
      }

      return <WrappedComponent {...this.props} />
    }
  }
}
